package org.example.elm_xiangmu.service.impl;


import org.example.elm_xiangmu.mapper.CartMapper;
import org.example.elm_xiangmu.po.Business;
import org.example.elm_xiangmu.po.Cart;
import org.example.elm_xiangmu.po.Food;
import org.example.elm_xiangmu.po.OrderDetailet;
import org.example.elm_xiangmu.service.CartService;
import org.example.elm_xiangmu.service.FoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;

@Service
public class CartServiceImpl  implements CartService {
    @Autowired
    CartMapper cartMapper;
    @Autowired
    FoodService foodService;

    @Override
    public List<Cart> listCart(String userId, Integer businessId) {
        if(businessId == 0){
           List<Cart>  cartList=cartMapper.listCartAll(userId);
            for (Cart cart : cartList) {
                Food food = foodService.getByFoodId(cart.getFoodId());
                cart.setFood(food);
            }
            return cartList;

        }else {
            List<Cart>  cartList = cartMapper.listCartByBusinessId(userId,businessId);
            for (Cart cart : cartList) {
                Food food = foodService.getByFoodId(cart.getFoodId());
                cart.setFood(food);
            }
            return cartList;
        }

    }

    @Override
    public void saveCart(Cart cart) {
        cartMapper.saveCart(cart);

    }

    @Override
    public void updateCart(Cart cart) {
        System.out.println(cart);
        cartMapper.updateCart(cart);
    }

    @Override
    public boolean remove(Cart cart) {
        if(cart.getFoodId() != 0){
            cartMapper.removeByFoodId(cart);
            return true;
        }else {
            cartMapper.removeAll(cart);
            return true;
        }
    }
}
